package com.example.luyao.faceserver;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.io.IOException;

import fi.iki.elonen.NanoHTTPD;
import com.iim.recognition.caffe.LoadLibraryModule;

public class MainActivity extends AppCompatActivity {
    private final static String TAG = MainActivity.class.getCanonicalName();
    private HttpServer mHttpServer = null;
    private LoadLibraryModule loadLibraryModule;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(TAG, "lifecycle: onCreate");

        if (loadLibraryModule == null) {
            loadLibraryModule = LoadLibraryModule.getInstance();
            loadLibraryModule.recognition_start();
        }

        try {
            mHttpServer = new HttpServer(5055, this);
            mHttpServer.start(NanoHTTPD.SOCKET_READ_TIMEOUT, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "lifecycle: onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "lifecycle: onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "lifecycle: onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "lifecycle: onStop");
    }

    @Override
    protected void onDestroy() {
        Log.e(TAG, "lifecycle: onDestroy");
        if(mHttpServer != null) {
            mHttpServer.closeDB();
            mHttpServer.stop();
        }
        super.onDestroy();
    }
}
